<?php
namespace App\Service\LogisticService;

use App\Service\BaseService;
use App\Service\Code\Normal;
use Hyperf\Utils\WaitGroup;
use App\Service\AppService\SystemCacheConfigService;
use App\Service\VendorClient\OrderLogisticSearchApi\OrderLogisticSearchApiClient;
use App\Service\CacheService\CacheProviderService;
use App\Model\PssModel\ExpressWeiXin;
use App\Model\PssModel\Express;

/**
 * 物流轨迹查询服务
 * @author chenshubo
 */
class LogisticService extends BaseService
{  
    const CACHE_KEY = 'order_logistic_express_query_';
    
    /**
     * 物流轨迹批量查询服务
     * @param array $param
     * @return mixed
     */
    public function queryBatchLogisticList( array $param = [] )
    {
        if( empty($param['LogisticNoList']) ) {
            return Normal::returnCode(Normal::FAIL, "LogisticNoList参数不能为空");
        }
        
        $LogisticNoList = array_filter(array_unique($param['LogisticNoList']));
        if( empty($LogisticNoList) ) {
            return Normal::returnCode(Normal::FAIL, "LogisticNoList数据值不能为空");
        }
        $config = SystemCacheConfigService::instance()->getCacheInfoBySystemCode('kuaidi100');
        if( empty($config) ) {
            return Normal::returnCode(Normal::FAIL, "快递100系统未配置，请登录云仓->系统配置->业务配置->快递100进行配置!");
        }
        
        //多个物流单号同时处理
        $LogisticListCount = count($LogisticNoList);
        $channelCo = new WaitGroup();
        $channelCo->add($LogisticListCount);
        $resultLogisticQueryList = [];
        $resultLogisticErrorList = [];
        foreach ($LogisticNoList as $LogisticNo)
        {
            co(function () use ($channelCo,$LogisticNo,$config,& $resultLogisticQueryList,& $resultLogisticErrorList)
            {
                $cache_key = self::CACHE_KEY.$LogisticNo;
                $orderLogistic = CacheProviderService::instance()->getCacheByKey($cache_key);
                if( empty($orderLogistic) ) {
                    $orderLogistic = (new OrderLogisticSearchApiClient($config))->QueryLogisticByExpressSn($LogisticNo);
                    CacheProviderService::instance()->cacheBySetKeyValue($cache_key,$orderLogistic);
                }
                
                if( is_array($orderLogistic) ) {
                    $resultLogisticQueryList[] = [
                        'LogisticNo'    => $LogisticNo,
                        'LogisticList'  => $orderLogistic
                    ];
                } else {
                    $resultLogisticErrorList[] = [
                        'LogisticNo'    => $LogisticNo,
                        'LogisticList'  => $orderLogistic
                    ];
                }
                $channelCo->done();
            });
        }
        $channelCo->wait();
        
        if( !empty($resultLogisticErrorList) ) {
            return Normal::returnCode(Normal::FAIL,"物流获取失败",$resultLogisticErrorList);
        }
        
        return Normal::returnCode(Normal::SUC,"获取成功",$resultLogisticQueryList);
    }
    
    
    /**
     * 绑定物流信息
     * @param array $param
     * @return mixed
     */
    public function BindBbcExpress( array $param = [] )
    {
        if( empty($param['ID']) ) {
            return Normal::returnCode(Normal::FAIL, "关联ID不能为空");
        }

        if( empty($param['bbcWxName']) ) {
            return Normal::returnCode(Normal::FAIL, "渠道物流公司不能为空");
        }

        if( empty($param['bbcWxCode']) ) {
            return Normal::returnCode(Normal::FAIL, "渠道物流编码不能为空");
        }
        $id = $param['ID'];
        
        $express=[
            'id'                    => $id,
            'bbc_wx_name'           => $param['bbcWxName'],
            'bbc_wx_code'           => $param['bbcWxCode'],
        ];
        
        $res = Express::instance()->info($express);
        if($res) {
            return Normal::returnCode(Normal::SUC,"关联成功");
        }
        
        $result = Express::instance()->where("id",$id)->update($express);
        if(!$result){
            return Normal::returnCode(Normal::SUC,"关联失败");
        }
        return Normal::returnCode(Normal::SUC,"关联成功");
    }
    /**
     * 批量查询云仓所有物流
     * @param array $param
     * @return mixed
     */
    public function QueryExpressList( array $param = [] )
    {
        $where = [];
        if(isset($param["id"]) && $param["id"]){
            $id = $param["id"];
            $where[]=["id","=","{$id}"];
        }
        $expressList=Express::instance()->where($where)->select("id as express_id","name as express_name")->get()->toArray();
        return Normal::returnCode(Normal::SUC,"获取成功",$expressList);
    }
    /**
     * 批量查询云仓物流信息
     * @param array $param
     * @return mixed
     */
    public function QueryYcExpressList( array $param = [] )
    {
        $where = [];
        if(isset($param["expressSupplierName"]) && $param["expressSupplierName"]){
            $expressSupplierName = $param["expressSupplierName"];
            $where[]=["name","like","%{$expressSupplierName}%"];
        }
        $expressList=Express::instance()->where($where)->select("id as ID","name as expressSupplierName","bbc_wx_name as bbcWxName","bbc_wx_code as bbcWxCode")->get()->toArray();
        return Normal::returnCode(Normal::SUC,"获取成功",$expressList);
    }
    
    /**
     * 批量查询微信物流信息
     * @param array $param
     * @return mixed
     */
    public function QueryWxExpressList( array $param = [] )
    {
        $where = [];
        if(isset($param["expressName"]) && $param["expressName"]){
            $express_name = $param["expressName"];
            $where[]=["express_name","like","%{$express_name}%"];
        }
        $expressList=ExpressWeiXin::instance()->where($where)->get()->toArray();
        return Normal::returnCode(Normal::SUC,"获取成功",$expressList);
    }
}